﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class Manage_basisset_ClassExerises : AdminPageBase
{
    protected int classId;
  
    protected void Page_Load(object sender, EventArgs e)
    {
        CheckAdmin();
        GetUserPermisions();
    
        GetClassId();
        if (!IsPostBack)
        {
            BindExerisesType();
            if (add == 1)
            {
                btnsave.Enabled = true;
            }
            else
            {
                btnsave.Enabled = false;
            }
        }
       
    }
    protected void CheckAdmin()
    {
        if (AdminSessionHelper.UserDetails == null)
        {
            JsHelper.TopAlertRedirect("请先登录", "../login.aspx", this.Page);
        }


    }
    protected void BindExerisesType()
    {
        DataSet ds = jtDataAccess.GetList("ExercisesType", "1=1", "ExercisesTypeId");
        if (CoreTools.CheckDataSet(ds))
        {
            ddlExerisesType.DataSource = ds;
            ddlExerisesType.DataTextField = "Name";
            ddlExerisesType.DataValueField = "ExercisesTypeId";
            ddlExerisesType.DataBind();

        }
        ListItem item = new ListItem("=请选择题型=", "0");
        ddlExerisesType.Items.Insert(0, item);
    }
    protected void GetClassId()
    {
        if (Request.QueryString["id"] != null)
        {
            classId = Convert.ToInt32(Request.QueryString["id"]);
            BindClassExerises();
        }

    }
    protected void BindClassExerises()
    {
        DataSet ds = jtDataAccess.GetExercises(classId);
        if (CoreTools.CheckDataSet(ds))
        {

            // GridView绑定并显示数据
            //this.GridView1.DataSource = ds.Tables[0];
            //this.GridView1.DataBind();
            GridviewControl.GridViewDataBind(this.GridView1, ds.Tables[0]);
        }
        else
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("ExercisesId");
            dt.Columns.Add("ExercisesTypeName");
            dt.Columns.Add("SN");
            dt.Columns.Add("Answer");
            dt.Columns.Add("Score");
            dt.Columns.Add("Question");
            dt.Columns.Add("");

            GridviewControl.GridViewDataBind(this.GridView1, dt);
        }
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //首先判断是否是数据行
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //当鼠标停留时更改背景色
            e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FCFDEE'");
            //当鼠标移开时还原背景色
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
        }
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        int flag = 0;
        if (e.CommandName == "modify")
        {
           // Response.Redirect("ExercisesDetail.aspx?id=" + int.Parse(e.CommandArgument.ToString()) + "&classid="+classId+"&MenuId=" + MenuId);
            hdfExercisesId.Value = e.CommandArgument.ToString();
            BindExerises(int.Parse(e.CommandArgument.ToString()));
           
        }
        else if (e.CommandName == "Del")
        {

            int ExercisesId = int.Parse(e.CommandArgument.ToString());

            flag = jtDataAccess.OpExercises(JTDataAccess.OpType.Delete, ExercisesId, classId, 0, 0, "", "", "", "", "", "", 0);

            switch (flag)
            {

                case -1:
                    ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("该练习已经在使用，不允许删除！"), false);
                    break;
                case 0:
                    ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("删除失败！"), false);
                    break;
                default:
                    ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("删除成功！"), false);
                    BindClassExerises();
                    break;
            }

        }
    }
    protected void btnsave_Click(object sender, EventArgs e)
    {
        int Exercisesid = Convert.ToInt32(hdfExercisesId.Value);
        string Question = txtQuestion.Text.Trim();
        string ChoseA = txtChoseA.Text.Trim();
        string ChoseB = txtChoseB.Text.Trim();
        string ChoseC = txtChoseC.Text.Trim();
        string ChoseD = txtChoseD.Text.Trim();
        string Answer = txtAnswer.Text.Trim();
        int SN = Convert.ToInt32(txtSN.Text.Trim());
        int ExercisesTypeId = Convert.ToInt32(ddlExerisesType.SelectedValue);
        decimal Score = decimal.Parse(txtScore.Text.Trim());
        if (!CoreTools.ProcessSqlStr(Question, 0))
        {
            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert(ConfigHelper.ProcessSqlStrErroMsg), false);
            txtQuestion.Focus();
        }
        else if (!CoreTools.ProcessSqlStr(ChoseA, 0))
        {
            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert(ConfigHelper.ProcessSqlStrErroMsg), false);
            txtChoseA.Focus();
        }
        else if (!CoreTools.ProcessSqlStr(ChoseB, 0))
        {
            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert(ConfigHelper.ProcessSqlStrErroMsg), false);
            txtChoseB.Focus();
        }
        else if (!CoreTools.ProcessSqlStr(ChoseC, 0))
        {
            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert(ConfigHelper.ProcessSqlStrErroMsg), false);
            txtChoseC.Focus();
        }
        else if (!CoreTools.ProcessSqlStr(ChoseD, 0))
        {
            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert(ConfigHelper.ProcessSqlStrErroMsg), false);
            txtChoseD.Focus();
        }
        else if (!CoreTools.ProcessSqlStr(Answer, 0))
        {
            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert(ConfigHelper.ProcessSqlStrErroMsg), false);
            txtAnswer.Focus();
        }
        else
        {
            JTDataAccess.OpType OpType;
            if (Exercisesid == 0)
            {
                OpType = JTDataAccess.OpType.Insert;

            }
            else
            {
                OpType = JTDataAccess.OpType.Update;

            }

            int flag = jtDataAccess.OpExercises(OpType, Exercisesid, classId, ExercisesTypeId, SN, Question, ChoseA, ChoseB, ChoseC, ChoseD, Answer, Score);

            switch (flag)
            {
                case -1:
                    ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("操作失败，已经存在相同名称或编号的练习题！"), false);

                    break;
                case 0:
                    ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("操作失败,请确认课程基本信息已保存！"), false);
                    break;
                default:
                    ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("操作成功！"), false);
                    BindClassExerises();
                    break;
            }
        }
    }
    protected void BindExerises(int exerisesid)
    {
        if (exerisesid != 0)
        {
            DataSet ds = jtDataAccess.GetList("Exercises", "ExercisesId=" + exerisesid, "ExercisesId");
            if (CoreTools.CheckDataSet(ds))
            {
                ddlExerisesType.SelectedValue = ds.Tables[0].Rows[0]["ExercisesTypeId"].ToString();
                int typeid = Convert.ToInt32(ds.Tables[0].Rows[0]["ExercisesTypeId"].ToString());
                if (typeid == 1)
                {
                    txtChoseA.Enabled = true;
                    txtChoseB.Enabled = true;
                    txtChoseC.Enabled = true;
                    txtChoseD.Enabled = true;
                    txtAnswer.Enabled = true;
                    rblselect.Enabled = false;
                    txtAnswer.Text = ds.Tables[0].Rows[0]["Answer"].ToString();
                    txtChoseA.Text = ds.Tables[0].Rows[0]["ChoseA"].ToString();
                    txtChoseB.Text = ds.Tables[0].Rows[0]["ChoseB"].ToString();
                    txtChoseC.Text = ds.Tables[0].Rows[0]["ChoseC"].ToString();
                    txtChoseD.Text = ds.Tables[0].Rows[0]["ChoseD"].ToString();
                }
                else
                {
                    txtChoseA.Enabled = false;
                    txtChoseB.Enabled = false;
                    txtChoseC.Enabled = false;
                    txtChoseD.Enabled = false;
                    rblselect.Enabled = true;
                    txtChoseA.Text = "";
                    txtChoseB.Text = "";
                    txtChoseC.Text = "";
                    txtChoseD.Text = "";
                    rblselect.SelectedValue = ds.Tables[0].Rows[0]["Answer"].ToString();
                }
                txtQuestion.Text = ds.Tables[0].Rows[0]["Question"].ToString();
                txtScore.Text = ds.Tables[0].Rows[0]["Score"].ToString();
                txtSN.Text = ds.Tables[0].Rows[0]["SN"].ToString();
                

            }
        }
    }
    protected void btnAddExerises_Click(object sender, EventArgs e)
    {
        ClearForm();
    }
    protected void ClearForm()
    {
        txtAnswer.Text = "";
        txtChoseA.Text = "";
        txtChoseB.Text = "";
        txtChoseC.Text = "";
        txtChoseD.Text = "";
        txtQuestion.Text = "";
        txtScore.Text = "";
        txtSN.Text = "";
        ddlExerisesType.SelectedValue = "0";
       hdfExercisesId.Value = "0";
    }
    protected void ddlExerisesType_SelectedIndexChanged(object sender, EventArgs e)
    {
        int typeid = Convert.ToInt32(ddlExerisesType.SelectedValue);
        if (typeid == 1)
        {
            txtChoseA.Enabled = true;
            txtChoseB.Enabled = true;
            txtChoseC.Enabled = true;
            txtChoseD.Enabled = true;
            txtAnswer.Enabled = true;
            txtAnswer.Text = "";
            rblselect.Enabled = false;
        }
        else
        {
            txtChoseA.Enabled = false;
            txtChoseB.Enabled = false;
            txtChoseC.Enabled = false;
            txtChoseD.Enabled = false;
            txtAnswer.Text = "对";
            txtAnswer.Enabled = false;
            rblselect.Enabled = true;
        }
    }
    protected void rblselect_SelectedIndexChanged(object sender, EventArgs e)
    {
        txtAnswer.Text = rblselect.SelectedValue;
    }
}